package gao.xiaolei.service;

import gao.xiaolei.dto.CopyJobInDto;
import gao.xiaolei.dto.JobInDtoBase;
import gao.xiaolei.dto.TaskJobInDto;
import gao.xiaolei.quertz.*;
import gao.xiaolei.util.CronUtil;
import org.quartz.Job;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Service("TimingService")
public class TimingService {

    @Resource(name = "QuartzJobService")
    private QuartzJobService quartzJobService;

    //添加复制定时任务
    public void addTestCopyTimingJob(CopyJobInDto copyJobInDto) throws SchedulerException {
        Map<String, Object> trqCopyData = new HashMap();
        trqCopyData.put("type", copyJobInDto.getType());
        JobKey jobKey = JobKey.jobKey(copyJobInDto.getKey(), copyJobInDto.getGroup());
        cancelJob(jobKey);//先去判断这个定时任务是否存在，如果存在就去删除他
        createJob(jobKey, trqCopyData, copyJobInDto.getBeginTime(), CopyWorkJob.class, copyJobInDto.getDescription());
    }

    //添加向某人发送任务的定时任务
    public void addSendTaskJob(TaskJobInDto taskJobInDto) throws SchedulerException {
        Map<String, Object> trqCopyData = new HashMap();
        trqCopyData.put("task", taskJobInDto.getTaskInDto());
        JobKey jobKey = JobKey.jobKey(taskJobInDto.getKey(), taskJobInDto.getGroup());
        cancelJob(jobKey);//先去判断这个定时任务是否存在，如果存在就去删除他
        createJob(jobKey, trqCopyData, taskJobInDto.getBeginTime(), SendTaskJob.class, taskJobInDto.getDescription());
    }

    //添加班级到了开班截止时间的定时任务
    public void addClassesOpenJob(JobInDtoBase jobInDtoBase) throws SchedulerException {
        Map<String, Object> trqCopyData = new HashMap();
        JobKey jobKey = JobKey.jobKey(jobInDtoBase.getKey(), jobInDtoBase.getGroup());
        cancelJob(jobKey);//先去判断这个定时任务是否存在，如果存在就去删除他
        createJob(jobKey, null, jobInDtoBase.getBeginTime(), ClassesOpenJob.class, jobInDtoBase.getDescription());
    }

    //取消定时任务
    public void cancelJob(JobKey jobKey) throws SchedulerException {
        if (quartzJobService.checkJobExit(jobKey)) {
            quartzJobService.pauseJob(jobKey);
            quartzJobService.deleteJob(jobKey);
        }
    }

    //根据组来取消定时任务
    public void cancelJobByGroup(String group) throws SchedulerException {
        Set<JobKey> jobKeys = quartzJobService.getKeyByGroup(group);
        for (JobKey j : jobKeys) {
            quartzJobService.pauseJob(j);
            quartzJobService.deleteJob(j);
        }
    }

    //创建定时任务
    private void createJob(JobKey jobKey, Map<String, Object> dataMap, LocalDateTime doTime, Class<? extends Job> jobClass, String description) throws SchedulerException {
        TaskDefine taskDefine = TaskDefine.builder()
                .jobKey(jobKey)
                .cronExpression(CronUtil.getCron(doTime))   //定时任务 的cron表达式
                .jobClass(jobClass)   //定时任务 的具体执行逻辑
                .description(description)    //定时任务 的描述
                .jobDataMap(dataMap)
                .build();
        quartzJobService.scheduleJob(taskDefine);
    }

}
